package edu.sjtu.erplab.cm.util;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class XMLParser {

	private Map<String, String> changeMap = new HashMap<String, String>();
	private int changeMapSize = 0;

	@SuppressWarnings({ "rawtypes" })
	public XMLParser(String changeXML) {

		try {
			StringReader strReader = new StringReader(changeXML);
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(strReader);
			Element root = document.getRootElement();
			List components = root.elements("component");
			for (Iterator it = components.iterator(); it.hasNext();) {
				Element component = (Element) it.next();
				this.changeMap.put(component.attributeValue("oldname"),
						component.attributeValue("newname"));

			}
			this.changeMapSize = changeMap.size();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String changeJpdlXml(String oldJpdlXml) {
		String newJpdlXml = "";
		try {
			// 命名空间处理
			Map<String, String> map = new HashMap<String, String>();
			map.put("xmlns", "http://jbpm.org/4.4/jpdl");
			StringReader strReader = new StringReader(oldJpdlXml);
			SAXReader reader = new SAXReader();
			Document document = reader.read(strReader);
			//
			XPath x = document.createXPath("//xmlns:transition/@to");
			x.setNamespaceURIs(map);
			List list = x.selectNodes(document);
			Iterator iter = list.iterator();
			System.out.println(list.size());
			while (iter.hasNext()) {
				Attribute attribute = (Attribute) iter.next();
				System.out.println(attribute.getValue());
				if (changeMap.containsKey(attribute.getValue()))
					attribute.setValue(changeMap.get(attribute.getValue()));
			}

			//
			Map<String, String> transitionMap = new HashMap<String, String>();
			for (Iterator<String> i = changeMap.keySet().iterator(); i
					.hasNext();) {
				String key = i.next().toString();
				transitionMap.put("to " + key, "to " + changeMap.get(key));
			}

			x = document.createXPath("//xmlns:transition/@name");
			x.setNamespaceURIs(map);
			list = x.selectNodes(document);
			iter = list.iterator();
			System.out.println(list.size());

			while (iter.hasNext()) {
				Attribute attribute = (Attribute) iter.next();
				System.out.println(attribute.getValue());
				if (transitionMap.containsKey(attribute.getValue())) {
					attribute.setValue(transitionMap.get(attribute.getValue()));
					System.out.println("ffffffffffff "
							+ transitionMap.get(attribute.getValue()));

				}

			}

			//
			x = document.createXPath("//xmlns:java/@name");
			x.setNamespaceURIs(map);
			list = x.selectNodes(document);
			iter = list.iterator();
			System.out.println(list.size());
			while (iter.hasNext()) {
				Attribute attribute = (Attribute) iter.next();
				System.out.println(attribute.getValue());
				if (changeMap.containsKey(attribute.getValue()))
					attribute.setValue(changeMap.get(attribute.getValue()));
			}

			newJpdlXml = document.asXML();
			System.out.println(document.asXML());
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newJpdlXml;

	}

	public String changeComponentsXml(String oldComponentsXml) {
		String newComponentsXml = "";
		try {
			// 命名空间处理
			StringReader strReader = new StringReader(oldComponentsXml);
			SAXReader reader = new SAXReader();
			Document document = reader.read(strReader);
			List list = document.selectNodes("//componentName");
			Iterator iter = list.iterator();
			System.out.println(list.size());
			while (iter.hasNext()) {
				Element element = (Element) iter.next();
				System.out.println(element.getText());
				if (changeMap.containsKey(element.getText()))
					element.setText(changeMap.get(element.getText()));

			}
			newComponentsXml = document.asXML();
			System.out.println(document.asXML());
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return newComponentsXml;

	}

	public String changeOriginalJpdl(String oldOriginalJpdl) {
		String newOriginalJpdl = "";
		try {
			// 命名空间处理
			Map<String, String> map = new HashMap<String, String>();
			map.put("xmlns", "http://jbpm.org/4.3/jpdl");
			StringReader strReader = new StringReader(oldOriginalJpdl);
			SAXReader reader = new SAXReader();
			Document document = reader.read(strReader);
			XPath x = document.createXPath("//xmlns:component/@name");
			x.setNamespaceURIs(map);
			List list = x.selectNodes(document);
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				Attribute attribute = (Attribute) iter.next();
				if (changeMap.containsKey(attribute.getValue()))
					attribute.setValue(changeMap.get(attribute.getValue()));

			}

			//
			x = document.createXPath("//xmlns:transition/@to");
			x.setNamespaceURIs(map);
			list = x.selectNodes(document);
			iter = list.iterator();
			while (iter.hasNext()) {
				Attribute attribute = (Attribute) iter.next();
				if (changeMap.containsKey(attribute.getValue()))
					attribute.setValue(changeMap.get(attribute.getValue()));
			}

			//
			x = document.createXPath("//xmlns:transition/@name");
			x.setNamespaceURIs(map);
			list = x.selectNodes(document);
			iter = list.iterator();
			while (iter.hasNext()) {
				Attribute attribute = (Attribute) iter.next();
				String[] namesplits = attribute.getValue().split("_");
				if (changeMap.containsKey(namesplits[0]))
					namesplits[0] = changeMap.get(namesplits[0]);
				if (changeMap.containsKey(namesplits[1]))
					namesplits[1] = changeMap.get(namesplits[1]);
				attribute.setValue(namesplits[0] + "_" + namesplits[1]);

			}

			newOriginalJpdl = document.asXML();
			System.out.println(document.asXML());
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newOriginalJpdl;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String originalJpdlXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><process name=\"cc\" xmlns=\"http://jbpm.org/4.3/jpdl\"><start g=\"103,290,30,30\" name=\"start1\" ><transition g=\"0,0\" name=\"start1_订单录入(锐特)\" to=\"订单录入(锐特)\"/></start><end g=\"975,281,30,30\" name=\"end1\" ></end><component g=\"235,287,30,30\" name=\"订单录入(锐特)\" assignee=\"订单录入(锐特)\" arguments=\"eabfa5a0-1ec0-43c9-ba78-3ee4509caad6###42007###订单录入(锐特)\"><transition g=\"0,0\" name=\"订单录入(锐特)_订单审核(锐特)\" to=\"订单审核(锐特)\"/></component><component g=\"331,297,30,30\" name=\"订单审核(锐特)\" assignee=\"订单审核(锐特)\" arguments=\"3a659275-6c09-4fc5-b018-34a8cf3729a6###42001###订单审核(锐特)\"><transition g=\"0,0\" name=\"订单审核(锐特)_散货调度(锐特)\" to=\"散货调度(锐特)\"/></component><component g=\"559,289,30,30\" name=\"派车跟踪(锐特)\" assignee=\"派车跟踪(锐特)\" arguments=\"91f1ba2d-d4c9-4107-ae2f-69a82074bf24###42003###派车跟踪(锐特)\"><transition g=\"0,0\" name=\"派车跟踪(锐特)_订单配载\" to=\"订单配载\"/></component><component g=\"823,324,30,30\" name=\"订单录入(未名)\" assignee=\"订单录入(未名)\" arguments=\"b5613122-425d-4227-b061-17bb09ee8878###42000###订单录入(未名)\"><transition g=\"0,0\" name=\"订单录入(未名)_end1\" to=\"end1\"/></component><component g=\"687,272,30,30\" name=\"订单配载\" assignee=\"订单配载(未名)\" arguments=\"6099a21a-1038-492c-b877-9d474bc9e36a###42005###订单配载(未名)\"><transition g=\"0,0\" name=\"订单配载_订单录入(未名)\" to=\"订单录入(未名)\"/></component><component g=\"444,269,30,30\" name=\"散货调度(锐特)\" assignee=\"散货调度(锐特)\" arguments=\"619f986c-9e2b-4415-8c1c-6ebf0be133d6###42002###散货调度(锐特)\"><transition g=\"0,0\" name=\"散货调度(锐特)_派车跟踪(锐特)\" to=\"派车跟踪(锐特)\"/></component></process>";

		String componentXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<Services>"
				+ "<processKey>098b2b05-0436-464e-9e6e-4ed70fb8d4c1</processKey>"
				+ "<component>"
				+ "<componentNo>eabfa5a0-1ec0-43c9-ba78-3ee4509caad6</componentNo>"
				+ "<beforeComponentNo>0000000</beforeComponentNo>"
				+ "<nextComponentNo>3a659275-6c09-4fc5-b018-34a8cf3729a6</nextComponentNo>"
				+ "<outputInterface>output</outputInterface>"
				+ "<serviceTag></serviceTag>"
				+ "<serviceURL>http://172.30.0.50:18088/TMS_DEV_SAAS_TEST/services/OrderInputWebservices?wsdl</serviceURL>"
				+ "<httpURL>http://172.30.0.50:18088/TMS_DEV_SAAS_TEST/saasGuide.jsp?module=orderEditor</httpURL>"
				+ "<serviceName>海公铁多式联运信息服务</serviceName>"
				+ "<servicePort></servicePort>"
				+ "<namespaceURL></namespaceURL>"
				+ "<componentTitle>订单录入(锐特)</componentTitle>"
				+ "<componentName>订单录入(锐特)</componentName>"
				+ "<inputInterface>input</inputInterface>"
				+ "</component>"
				+ "<component>"
				+ "<componentNo>3a659275-6c09-4fc5-b018-34a8cf3729a6</componentNo>"
				+ "<beforeComponentNo>eabfa5a0-1ec0-43c9-ba78-3ee4509caad6</beforeComponentNo>"
				+ "<nextComponentNo>619f986c-9e2b-4415-8c1c-6ebf0be133d6</nextComponentNo>"
				+ "<outputInterface>output</outputInterface>"
				+ "<serviceTag></serviceTag>"
				+ "<serviceURL>http://172.30.0.50:18088/TMS_DEV_SAAS_TEST/services/OrderInputWebservices?wsdl</serviceURL>"
				+ "<httpURL>http://172.30.0.50:18088/TMS_DEV_SAAS_TEST/saasGuide.jsp?module=audit</httpURL>"
				+ "<serviceName>海公铁多式联运信息服务</serviceName>"
				+ "<servicePort></servicePort>"
				+ "<namespaceURL></namespaceURL>"
				+ "<componentTitle>订单审核(锐特)</componentTitle>"
				+ "<componentName>订单审核(锐特)</componentName>"
				+ "<inputInterface>input</inputInterface>" + "</component>"
				+ "</Services>";

		String jpdlxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<process xmlns=\"http://jbpm.org/4.4/jpdl\" name=\"098b2b05-0436-464e-9e6e-4ed70fb8d4c1\">"
				+ "<start g=\"49,216,48,48\" name=\"start\">"
				+ "<transition g=\"-67,-37\" name=\"to start service flow\" to=\"component point\"/></start>"
				+ "<decision g=\"195,215,48,48\" name=\"component point\">"
				+ "<handler class=\"com.flowframe.service.ComponentTaskDecision\"/>"
				+ "<transition g=\"-53,-17\" name=\"to 订单录入(锐特)\" to=\"订单录入(锐特)\"/>"
				+ "<transition g=\"-53,-17\" name=\"to 订单审核(锐特)\" to=\"订单审核(锐特)\"/>"
				+ "<transition g=\"-53,-17\" name=\"to 散货调度(锐特)\" to=\"散货调度(锐特)\"/>"
				+ "<transition g=\"-53,-17\" name=\"to 派车跟踪(锐特)\" to=\"派车跟踪(锐特)\"/>"
				+ "<transition g=\"-53,-17\" name=\"to 订单配载\" to=\"订单配载\"/>"
				+ "<transition g=\"-53,-17\" name=\"to 订单录入(未名)\" to=\"订单录入(未名)\"/>"
				+ "</decision>"
				+ "<java class=\"com.flowframe.service.ComponentClient\" g=\"412,69,105,52\" method=\"completeComponentTask\" name=\"订单录入(锐特)\">"
				+ "<arg><ref object=\"processKey\"/></arg>"
				+ "<arg><ref object=\"componentNo\"/></arg>"
				+ "<arg><ref object=\"xmldoc\"/></arg>"
				+ "<transition g=\"-47,-17\" name=\" to end\" to=\"end\"/>"
				+ "</java>"
				+ "<java class=\"com.flowframe.service.ComponentClient\" g=\"412,69,105,52\" method=\"completeComponentTask\" name=\"订单审核(锐特)\">"
				+ "<arg><ref object=\"processKey\"/></arg>"
				+ "<arg><ref object=\"componentNo\"/></arg>"
				+ "<arg><ref object=\"xmldoc\"/></arg>"
				+ "<transition g=\"-47,-17\" name=\" to end\" to=\"end\"/>"
				+ "</java>"
				+ "<java class=\"com.flowframe.service.ComponentClient\" g=\"412,69,105,52\" method=\"completeComponentTask\" name=\"散货调度(锐特)\">"
				+ "<arg><ref object=\"processKey\"/></arg><arg><ref object=\"componentNo\"/></arg>"
				+ "<arg><ref object=\"xmldoc\"/></arg>"
				+ "<transition g=\"-47,-17\" name=\" to end\" to=\"end\"/>"
				+ "</java>"
				+ "<java class=\"com.flowframe.service.ComponentClient\" g=\"412,69,105,52\" method=\"completeComponentTask\" name=\"派车跟踪(锐特)\">"
				+ "<arg><ref object=\"processKey\"/></arg>"
				+ "<arg><ref object=\"componentNo\"/></arg>"
				+ "<arg><ref object=\"xmldoc\"/></arg>"
				+ "<transition g=\"-47,-17\" name=\" to end\" to=\"end\"/>"
				+ "</java>"
				+ "<java class=\"com.flowframe.service.ComponentClient\" g=\"412,69,105,52\" method=\"completeComponentTask\" name=\"订单配载\">"
				+ "<arg><ref object=\"processKey\"/></arg>"
				+ "<arg><ref object=\"componentNo\"/></arg>"
				+ "<arg><ref object=\"xmldoc\"/></arg>"
				+ "<transition g=\"-47,-17\" name=\" to end\" to=\"end\"/>"
				+ "</java>"
				+ "<java class=\"com.flowframe.service.ComponentClient\" g=\"412,69,105,52\" method=\"completeComponentTask\" name=\"订单录入(未名)\">"
				+ "<arg><ref object=\"processKey\"/></arg>"
				+ "<arg><ref object=\"componentNo\"/></arg>"
				+ "<arg><ref object=\"xmldoc\"/></arg>"
				+ "<transition g=\"-47,-17\" name=\" to end\" to=\"end\"/>"
				+ "</java>" + "<end g=\"658,221,48,48\" name=\"end\"/>"
				+ "</process>";
		String changXml = "<ComponentChanges>"
				+ "<component oldname=\"订单录入(锐特)\" newname=\"订单录入(锐特)22\"/>"
				+ "<component oldname=\"订单审核(锐特)\" newname=\"订单审核(锐特)22\"/>"
				+ "<component oldname=\"派车跟踪(锐特)\" newname=\"派车跟踪(锐特)22\"/>"
				+ "<component oldname=\"订单录入(未名)\" newname=\"订单录入(未名)22\"/>"
				+ "</ComponentChanges>";
		XMLParser xmlParser = new XMLParser(changXml);
		// xmlParser.changeOriginalJpdl(xml);
		// xmlParser.changeComponentsXml(componentXml);
		xmlParser.changeJpdlXml(jpdlxml);
		// try {
		// // 命名空间处理
		// Map map = new HashMap();
		// map.put("xmlns", "http://jbpm.org/4.3/jpdl");
		// StringReader strReader = new StringReader(xml);
		// SAXReader reader = new SAXReader();
		// Document document = reader.read(strReader);
		// XPath x = document.createXPath("//xmlns:component/@name");
		// x.setNamespaceURIs(map);
		// List list = x.selectNodes(document);
		// Iterator iter = list.iterator();
		// System.out.println(list.size());
		// while (iter.hasNext()) {
		// Attribute attribute = (Attribute) iter.next();
		// System.out.println(attribute.getText());
		// // if (attribute.getValue().equals("订单录入(锐特)")) {
		// // System.out.println("dd");
		// // attribute.setValue("订单录入(锐特)2");
		// //
		// // }
		//
		// }
		// System.out.println(document.asXML());
		// } catch (DocumentException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}
}
